<template>
	<el-dialog title="角色权限设置" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
		<el-tabs tab-position="top">
			<el-tab-pane label="菜单权限">
				<div class="treeMain">
					<el-tree
						ref="menu" node-key="id"
						:data="menu.list"
						:props="menu.props" id="menu"
						show-checkbox></el-tree>
				</div>
			</el-tab-pane>
		</el-tabs>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>

export default {
	emits: ['success', 'closed'],
	data() {
		return {
			visible: false,
			isSaveing: false,
			roleInfo: {},
			dataTypeList: [],
			form: {
				dataType: '5',
				menuChecked: [],
			},
			menu: {
				list: [],
				props: {
					label: (data) => {
						return data.meta.title
					}
				}
			},
		}
	},
	mounted() {
	},
	methods: {
		open() {
			this.visible = true;
			return this
		},
		submit() {
			this.isSaveing = true;
			//选中的和半选的合并后传值接口
			let params = {
				'menu_power': this.$refs.menu.getCheckedNodes(),
				'role_id': this.roleId,
				'permissions': this.form.dataType
			};
			this.$API['admin.power'].store.post(params)
				.then((res) => {
					if (res.code === 0) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.msg, "提示", {type: 'error'})
					}
				})
				.catch((msg) => {
					this.$alert(msg, "提示", {type: 'error'})
				})
				.finally(() => {
					this.isSaveing = false;
				})
		},
		async getData() {
			this.isSaveing = true
			let res = await this.$API['admin.power'].list.get({role_id: this.roleId})
			this.roleInfo = res.data.role_info;
			this.menu.list = res.data.menu_tree;
			this.dataTypeList = res.data.role_data_type;
			this.form.menuChecked = res.data.menu_checked;
			this.form.dataType = (this.roleInfo.permissions | 1).toString();
			this.isSaveing = false
			this.$refs.menu.setCheckedKeys(this.form.menuChecked)
		},
		setData(data) {
			this.roleId = data.role_id;
			this.getData()
		}
	}
}
</script>

<style scoped>
.treeMain {
	height: 280px;
	overflow: auto;
	border: 1px solid #dcdfe6;
	margin-bottom: 10px;
}
</style>
